﻿using Domain.Entity;
using Domain.Entity.Fy;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
	public class EIRepository : IEIRepository
	{
		private readonly MyDbContext myDbContext;

		public EIRepository(MyDbContext myDbContext)
		{
			this.myDbContext = myDbContext;
		}

		public async Task<bool> AddEI(EI e)
		{
			myDbContext.Add(e);
			int zhi = await myDbContext.SaveChangesAsync();
			return zhi > 0 ? true : false;
		}


		public List<EI> GetEI()
		{
			List<EI> list = myDbContext.EI.ToList();
			return list;
		}
		public async Task<bool> UpdateEI(EI e)
		{
			var zhi = await myDbContext.EI.FindAsync(e.EinId);

			if (zhi == null)
			{
				return false;
			}

			zhi.InterviewAmount = e.InterviewAmount; //面试次数
			zhi.ImageDegree = e.ImageDegree; //形象等级
			zhi.NativeLanguageDegree = e.NativeLanguageDegree; //口才等级
			zhi.ForeignLanguageDegree = e.ForeignLanguageDegree;     //外语水平等级
			zhi.ResponseSpeedDegree = e.ResponseSpeedDegree;  //应变能力
			zhi.EQDegree = e.EQDegree;  //EQ
			zhi.IQDegree = e.IQDegree;  //IQ
			zhi.MultiQualityDegree = e.MultiQualityDegree;  //综合素质
			zhi.Register = e.Register;  //面试人
			zhi.RegisteTime = e.RegisteTime;  //面试时间
			zhi.InterviewComment = e.InterviewComment;  //面试评价
			zhi.InterviewStatus = e.InterviewStatus;  //面试状态
			zhi.Result = e.Result;  //面试结果

			myDbContext.EI.Update(zhi);
			int xg = await myDbContext.SaveChangesAsync();
			return xg > 0;
		}

		public async Task<bool> UpdateEICheckStatus(EI e)
		{
			var zhi = await myDbContext.EI.FindAsync(e.EinId);

			if (zhi == null)
			{
				return false;
			}
			zhi.CheckStatus = e.CheckStatus; //筛选状态
			zhi.CheckComment = e.CheckComment;//筛选评价
			zhi.CheckTime = e.CheckTime; //筛选时间
			zhi.Checker = e.Checker; //筛选人

			myDbContext.EI.Update(zhi);
			int xg = await myDbContext.SaveChangesAsync();
			return xg > 0;
		}
		public async Task<bool> DeleteEI(int id)
		{
			var zhi = await myDbContext.EI.SingleOrDefaultAsync(e => e.EinId == id);
			if (zhi != null)
			{
				myDbContext.EI.Remove(zhi);
				await myDbContext.SaveChangesAsync();
				return true;
			}
			return false;
		}

		public List<IAM> GetAllEI()
		{
			List<IAM> list = myDbContext.IAM.ToList();
			return list;
		}
		

		public IAMFy GetEIFY(int currentPage, int pageSize, string? checkStatus)
		{
			// 使用 Where 子句来过滤 IAM 实体
			IQueryable<IAM> filteredIAMs = myDbContext.IAM;

			if (!string.IsNullOrEmpty(checkStatus))
			{
				filteredIAMs = filteredIAMs.Where(iam => iam.CheckStatus == checkStatus);
			}

			// 应用分页
			List<IAM> eMRs = filteredIAMs.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

			// 计算总计数（考虑过滤条件）
			int totalCount = filteredIAMs.Count();

			// 创建并返回结果
			IAMFy result = new IAMFy() { IAMs = eMRs, TotalCount = totalCount };
			return result;
		}



		public List<IAM> GetIAMs()
		{
			List<IAM> list = myDbContext.IAM.ToList();
			return list;
		}

		public IAMFy GetEIFYPassPassComment(int currentPage, int pageSize, string PassPassComment)
		{
			// 使用 Where 子句来过滤 IAM 实体
			IQueryable<IAM> filteredIAMs = myDbContext.IAM;

			if (!string.IsNullOrEmpty(PassPassComment))
			{
				filteredIAMs = filteredIAMs.Where(iam => iam.PassPassComment == PassPassComment);
			}

			// 应用分页
			List<IAM> eMRs = filteredIAMs.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

			// 计算总计数（考虑过滤条件）
			int totalCount = filteredIAMs.Count();

			// 创建并返回结果
			IAMFy result = new IAMFy() { IAMs = eMRs, TotalCount = totalCount };
			return result;
		}



		public IAMFy GetEIFYTongyi(int currentPage, int pageSize, string PassPassComment)
		{
			// 使用 Where 子句来过滤 IAM 实体
			IQueryable<IAM> filteredIAMs = myDbContext.IAM;

			if (!string.IsNullOrEmpty(PassPassComment))
			{
				filteredIAMs = filteredIAMs.Where(iam => iam.PassCheckComment == PassPassComment);
			}

			// 应用分页
			List<IAM> eMRs = filteredIAMs.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

			// 计算总计数（考虑过滤条件）
			int totalCount = filteredIAMs.Count();

			// 创建并返回结果
			IAMFy result = new IAMFy() { IAMs = eMRs, TotalCount = totalCount };
			return result;
		}

	}
}
